package plantshop.controller.moves;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


public class MoveQueue implements Iterable<Move>, Serializable{
	private static final long serialVersionUID = -914382500481712578L;
	private Map<Integer, Move> moveQueue;
	private int key;
	private int start, end;
	private int size;
	
	public MoveQueue(){
		moveQueue = new HashMap<Integer, Move>(100);
		key = 0;
		start = 0;
		end = 0;
		size = 100;
	}
	
	public MoveQueue(int key, Map<Integer, Move> moveQueue){
		this.moveQueue = moveQueue;
		this.key = key;
	}
	
	public boolean isEmpty(){
		return size() == 0;
	}

	public Map<Integer, Move> getNext(){
		Map<Integer, Move> moves = new HashMap<Integer, Move>();
		for(int i = start; i < end; i++){
			moves.put(i, moveQueue.get(i));
		}
		reset();
		return moves;
	}
	
	public void reset(){
		for(;start < end; start++){
			moveQueue.put(start, null);
		}
		if(key < size){
			start = end;
		}
		else{
			start = 0;
			key = 0;
			end = 0;
		}
	}
	
	public void clear(){
		key = 0;
		moveQueue.clear();
	}
	
	public void add(Move move){
		moveQueue.put(key++, move);
		end++;
	}
	
	public void addAll(Map<Integer, Move> moves){
		for(int i:moves.keySet()){
			add(moves.get(i));
		}
	}
	
	public void addAll(MoveQueue moves){
		for(Move m: moves){
			add(m);
		}
	}
	
	public int size(){
		return end-start;
	}
	
	@Override
	public Iterator<Move> iterator() {
		return new MoveQueueIterator(moveQueue, start, end);
	}
	
	public static void main(String[] args) {
		MoveQueue queue = new MoveQueue();
		MoveQueue moves = new MoveQueue();
		for(int i = 0; i < 10; i++){
			//moves.add(new UnmarkMove(new TestNative()));
		}
		for(int i = 0; i < 10; i++){
			queue.addAll(moves);
		}
		queue.reset();
		queue.addAll(moves);
		for(Move m: queue){
			System.out.println(m);
		}
		System.out.println(queue.size());
	}
}
